local hx__jingce = fk.CreateSkill {
  name = "hx__jingce",
}


hx__jingce:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    local names = {}
    local room = player.room
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local c = Fk:getCardById(info.cardId)
            if c.type == Card.TypeBasic or c:isCommonTrick() then
              table.insertIfNeed(names, c.name)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    return target == player and player:hasSkill(hx__jingce.name) and #names > 0
  end,
  on_use = function(self, event, target, player, data)
    local names = {}
    local room = player.room
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local c = Fk:getCardById(info.cardId)
            if c.type == Card.TypeBasic or c:isCommonTrick() then
              table.insertIfNeed(names, c.name)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    local use = room:askToUseVirtualCard(player, {
      name = names,
      skill_name = hx__jingce.name,
      prompt = "#hx__jingce-choice",
      extra_data = {
        bypass_times = true,
        bypass_distances = true,
      },
      skip = true,
    })
    if use then
      local card = Fk:cloneCard(use.card.name)
      card.skillName = hx__jingce.name
      room:useCard{
        card = card,
        from = player,
        tos = use.tos,
        extraUse = true,
      }
      if not table.contains(player:getTableMark("@$hx__jingce"), Fk:cloneCard(use.card.name).trueName) then
        player:drawCards(1,hx__jingce.name)
      end
      room:addTableMark(player, "@$hx__jingce", Fk:cloneCard(use.card.name).name)
    end
  end,
})


hx__jingce:addEffect(fk.TurnEnd, {
  anim_type = "negative",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__jingce.name) and player:getTableMark("@$hx__jingce") ~= nil
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:askToDiscard(player, {
      skill_name = hx__jingce.name,
      min_num = #player:getTableMark("@$hx__jingce"),
      max_num = #player:getTableMark("@$hx__jingce"),
     cancelable = false,
    })
    room:setPlayerMark(player, "@$hx__jingce", 0)
  end,
})


return hx__jingce
